
package interfaz;

import datos.Jugador;
import java.util.Vector;
import javax.swing.JOptionPane;

public class VentanaSeleccionColorCliente extends javax.swing.JDialog {
    private VentanaPrincipal vp;
    
    public VentanaSeleccionColorCliente(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        vp = (VentanaPrincipal)parent;
        this.setLocationRelativeTo(null);
        this.setTitle("Selección de color");
        this.comprobarColores();
        vp.setVscc(this);
        jrbAmarillo.setSelected(false);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        jrbRojo = new javax.swing.JRadioButton();
        jbAceptar = new javax.swing.JButton();
        jrbVerde = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        jrbNaranja = new javax.swing.JRadioButton();
        jLabel5 = new javax.swing.JLabel();
        jrbAmarillo = new javax.swing.JRadioButton();
        jrbAzul = new javax.swing.JRadioButton();
        jrbNegro = new javax.swing.JRadioButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jlTexto = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/Colores/Azul.png"))); // NOI18N

        jrbRojo.setText("Disponible");
        jrbRojo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbRojoActionPerformed(evt);
            }
        });

        jbAceptar.setText("Aceptar");
        jbAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAceptarActionPerformed(evt);
            }
        });

        jrbVerde.setText("Disponible");
        jrbVerde.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbVerdeActionPerformed(evt);
            }
        });

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/Colores/Verde.png"))); // NOI18N

        jrbNaranja.setText("Disponible");
        jrbNaranja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbNaranjaActionPerformed(evt);
            }
        });

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/Colores/negro.png"))); // NOI18N

        jrbAmarillo.setSelected(true);
        jrbAmarillo.setText("Disponible");
        jrbAmarillo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbAmarilloActionPerformed(evt);
            }
        });

        jrbAzul.setText("Disponible");
        jrbAzul.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbAzulActionPerformed(evt);
            }
        });

        jrbNegro.setText("Disponible");
        jrbNegro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbNegroActionPerformed(evt);
            }
        });

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/Colores/Naranja.png"))); // NOI18N

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/Colores/Amarillo.png"))); // NOI18N

        jlTexto.setFont(new java.awt.Font("Tahoma", 0, 10));

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/Colores/Rojo.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jrbAmarillo)
                            .addComponent(jrbAzul)
                            .addComponent(jrbNaranja))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jrbRojo))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jrbNegro)
                                    .addComponent(jrbVerde)))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jlTexto, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
                        .addContainerGap())))
            .addGroup(layout.createSequentialGroup()
                .addGap(94, 94, 94)
                .addComponent(jbAceptar)
                .addContainerGap(103, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jrbRojo)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jrbVerde, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addComponent(jrbNegro)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jrbAmarillo)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jrbAzul, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jrbNaranja))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jlTexto, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbAceptar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jrbRojoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbRojoActionPerformed
        jrbAmarillo.setSelected(false);
        jrbAzul.setSelected(false);
        jrbNaranja.setSelected(false);
        jrbNegro.setSelected(false);
        jrbRojo.setSelected(true);
        jrbVerde.setSelected(false);
    }//GEN-LAST:event_jrbRojoActionPerformed

    private void jbAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAceptarActionPerformed
        if (jrbAmarillo.isSelected() || jrbAzul.isSelected() || jrbNaranja.isSelected() || jrbNegro.isSelected()
                || jrbRojo.isSelected() || jrbVerde.isSelected()){
            if(verificarNombre()) {
                if (jrbAmarillo.isSelected()) {
                    jrbAmarillo.setEnabled(false);
                    vp.getCliente().getJugador().setColor("Amarillo");
                    jrbAmarillo.setText(vp.getCliente().getJugador().getNombre());
                } else {
                    if (jrbAzul.isSelected()) {
                        jrbAzul.setEnabled(false);
                        vp.getCliente().getJugador().setColor("Azul");
                        jrbAzul.setText(vp.getCliente().getJugador().getNombre());
                    } else {
                        if (jrbNaranja.isSelected()) {
                            jrbNaranja.setEnabled(false);
                            vp.getCliente().getJugador().setColor("Naranja");
                            jrbNaranja.setText(vp.getCliente().getJugador().getNombre());
                        } else {
                            if (jrbNegro.isSelected()) {
                                jrbNegro.setEnabled(false);
                                vp.getCliente().getJugador().setColor("Negro");
                                jrbNegro.setText(vp.getCliente().getJugador().getNombre());
                            } else {
                                if (jrbRojo.isSelected()) {
                                    jrbRojo.setEnabled(false);
                                    vp.getCliente().getJugador().setColor("Rojo");
                                    jrbRojo.setText(vp.getCliente().getJugador().getNombre());
                                } else {
                                    jrbVerde.setEnabled(false);
                                    vp.getCliente().getJugador().setColor("Verde");
                                    jrbVerde.setText(vp.getCliente().getJugador().getNombre());

                                }
                            }
                        }
                    }
                }
            }
            jbAceptar.setEnabled(false);
            vp.getCliente().actualizarColor(vp.getCliente().getJugador().getNombre(), vp.getCliente().getJugador().getColor());
            this.comprobarColores();
            vp.getCliente().avisarColores();
            jlTexto.setText("Espere la conexion de los demas jugadores");
        } else {
            jlTexto.setText("Seleccione un color");
        }
    }//GEN-LAST:event_jbAceptarActionPerformed

    private void jrbVerdeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbVerdeActionPerformed
        jrbAmarillo.setSelected(false);
        jrbAzul.setSelected(false);
        jrbNaranja.setSelected(false);
        jrbNegro.setSelected(false);
        jrbRojo.setSelected(false);
        jrbVerde.setSelected(true);
    }//GEN-LAST:event_jrbVerdeActionPerformed

    private void jrbNaranjaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbNaranjaActionPerformed
        jrbAmarillo.setSelected(false);
        jrbAzul.setSelected(false);
        jrbNaranja.setSelected(true);
        jrbNegro.setSelected(false);
        jrbRojo.setSelected(false);
        jrbVerde.setSelected(false);
    }//GEN-LAST:event_jrbNaranjaActionPerformed

    private void jrbAmarilloActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbAmarilloActionPerformed
        jrbAmarillo.setSelected(true);
        jrbAzul.setSelected(false);
        jrbNaranja.setSelected(false);
        jrbNegro.setSelected(false);
        jrbRojo.setSelected(false);
        jrbVerde.setSelected(false);
    }//GEN-LAST:event_jrbAmarilloActionPerformed

    private void jrbAzulActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbAzulActionPerformed
        jrbAmarillo.setSelected(false);
        jrbAzul.setSelected(true);
        jrbNaranja.setSelected(false);
        jrbNegro.setSelected(false);
        jrbRojo.setSelected(false);
        jrbVerde.setSelected(false);
    }//GEN-LAST:event_jrbAzulActionPerformed

    private void jrbNegroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbNegroActionPerformed
        jrbAmarillo.setSelected(false);
        jrbAzul.setSelected(false);
        jrbNaranja.setSelected(false);
        jrbNegro.setSelected(true);
        jrbRojo.setSelected(false);
        jrbVerde.setSelected(false);
    }//GEN-LAST:event_jrbNegroActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JButton jbAceptar;
    private javax.swing.JLabel jlTexto;
    private javax.swing.JRadioButton jrbAmarillo;
    private javax.swing.JRadioButton jrbAzul;
    private javax.swing.JRadioButton jrbNaranja;
    private javax.swing.JRadioButton jrbNegro;
    private javax.swing.JRadioButton jrbRojo;
    private javax.swing.JRadioButton jrbVerde;
    // End of variables declaration//GEN-END:variables

    public void comprobarColores() {
        String[] nombresColores = vp.traeNombresYColores();
        String nombreUs = "";
        String colorUs = "";
        for(int i=0;i<nombresColores.length;i++){
           nombreUs = nombresColores[i];
           i++;
           colorUs = nombresColores[i];
           if(colorUs.compareToIgnoreCase("Amarillo") == 0){
               jrbAmarillo.setText(nombreUs);
               jrbAmarillo.setEnabled(false);
           }else{
               if(colorUs.compareToIgnoreCase("Azul") == 0){
                   jrbAzul.setText(nombreUs);
                   jrbAzul.setEnabled(false);
               }else{
                   if(colorUs.compareToIgnoreCase("Naranja") == 0){
                       jrbNaranja.setText(nombreUs);
                       jrbNaranja.setEnabled(false);
                   }else{
                       if(colorUs.compareToIgnoreCase("Negro") == 0){
                           jrbNegro.setText(nombreUs);
                           jrbNegro.setEnabled(false);
                       }else{
                           if(colorUs.compareToIgnoreCase("Rojo") == 0){
                               jrbRojo.setText(nombreUs);
                               jrbRojo.setEnabled(false);
                           }else{
                               if(colorUs.compareToIgnoreCase("Verde") == 0){
                                   jrbVerde.setText(nombreUs);
                                   jrbVerde.setEnabled(false);
                               }
                           }
                       }
                   }
               }
           }
        }
    }

    public void comenzarPartida() {
        this.dispose();
    }

    private boolean verificarNombre() {
//        String nombre = vp.getCliente().getJugador().getNombre();
//        if(jrbAmarillo.getText().equals(nombre) || jrbAzul.getText().equals(nombre) 
//                || jrbNaranja.getText().equals(nombre) || jrbNegro.getText().equals(nombre) 
//                || jrbRojo.getText().equals(nombre) || jrbVerde.getText().equals(nombre))
//            return true;
//        jlTexto.setText(nombre+"ya a sido utilizado por otro jugador");
//        return false;
        return true;
    }
}
